package com.lizy;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.lizy.entity.Position;
import org.junit.Test;

import java.nio.charset.StandardCharsets;
import java.util.Map;

public class FastJsonTest {

    /**
     * toJsonString测试
     */
    @Test
    public void testToJsonString() {
        Position position = new Position("1", "java开发", 10000);

        System.out.println("-------toJSONString(Object object)---------");
        String json1 = JSON.toJSONString(position);
        System.out.println("json:" + json1);

        // 使得输出语句更易读
        System.out.println("-------toJSONString(Object object, SerializerFeature... features)---------");
        String json2 = JSON.toJSONString(position, SerializerFeature.PrettyFormat);
        System.out.println("json:" + json2);
    }

    @Test
    public void tesToJsonBytes() {
        Position position = new Position("1", "java开发", 10000);

        System.out.println("-------toJsonBytes(Object object)---------");
        byte[] bytes1 = JSON.toJSONBytes(position);
        System.out.println("json:" + new String(bytes1, StandardCharsets.UTF_8));
    }

    @Test
    public void testParseObject() {
        Position position = new Position("1", "java开发", 10000);
        String json1 = JSON.toJSONString(position);

        System.out.println("--------parseObject-----------");
        JSONObject jsonObject = JSON.parseObject(json1);
        Map map = JSON.parseObject(json1, Map.class);
        Position position1 = JSON.parseObject(json1, Position.class);
        System.out.println("jsonObject: " + jsonObject);
        System.out.println("map: " + map);
        System.out.println("position:" + position1);
    }
}
